package com.example.lenovo.okhttphelpers;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.lenovo.okhttphelpers.bean.Banner;
import com.example.lenovo.okhttphelpers.http.OkHttpHelper;
import com.example.lenovo.okhttphelpers.http.SpotsCallBack;
import com.squareup.okhttp.Response;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {

    private OkHttpHelper httpHelper = OkHttpHelper.getInstance();// 初始化网络框架
    private Button mBt_access;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_view();
        init_listener();
    }

    private void init_view() {
        mBt_access = (Button) findViewById(R.id.bt_access_networks);
    }


    private void init_listener() {
        mBt_access.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_access_networks:
                String url ="http://112.124.22.238:8081/course_api/banner/query?type=1";

                httpHelper.get(url, new SpotsCallBack<List<Banner>>(MainActivity.this){
                    @Override
                    public void onSuccess(Response response, List<Banner> banners) {
                        StringBuilder sb=new StringBuilder();
                        for(Banner banner : banners){
                            sb.append(banner.toString());
                        }
                        Log.e("MainActivity",sb.toString());
                    }

                    @Override
                    public void onError(Response response, int code, Exception e) {
                        Log.e("MainActivity",code+"");
                    }

                    @Override
                    public void onTokenError(Response response, int code) {
                        Log.e("MainActivity",code+"");

                    }
                });
                break;
        }
    }
}
